﻿using System.Collections.Generic;
using System.Linq;

namespace Algoritmia.Schemes
{
    public interface IDivideAndConquerProblem<TSolution>
    {
        bool IsSimple { get;  }
        TSolution Combine(IEnumerable<TSolution> solutions);
        IEnumerable<IDivideAndConquerProblem<TSolution>> Divide();
        TSolution TrivialSolution();
    }

    public class DivideAndConquerSolver<TSolution>
    {
        public TSolution Solve(IDivideAndConquerProblem<TSolution> problem)
        {
            if (problem.IsSimple)
            {
                return problem.TrivialSolution();
            }
            else
            {
                return problem.Combine(problem.Divide().Select(p => Solve(p)));
            }
        }
    }
}
